package com.admin.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.goods.Sku;
import com.bayss.core.model.SkuQuery;
import com.bayss.core.service.sku.SkuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * sku
 */
@RestController
public class SkuController  extends ApiBaseController{
    @Autowired
    private SkuService skuService;

    /**
     * sku删除
     * @param id
     * @return
     */
    @GetMapping(value = "/sku/delete.json")
    public ResultMsg delete(Long id) {
        try {
            skuService.deleteById(id);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "删除失败");
        }
    }

    /**
     * sku新增
     * @param sku
     * @return
     */
    @PostMapping(value = "/sku/add.json")
    public ResultMsg add(@RequestBody Sku sku) {
        return skuService.add(sku);
    }

    /**
     * sku修改
     * @param sku
     * @return
     */
    @PostMapping(value = "/sku/update.json")
    public ResultMsg update(@RequestBody Sku sku) {
        skuService.update(sku);
        return ResultMsg.success();
    }

    /**
     * sku查询全部
     * @param sku
     * @return
     */
    @GetMapping(value = "/sku/list.json")
    public ResultMsg list(Sku sku) {
        try {
            return ResultMsg.success(skuService.list(sku));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * sku分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/sku/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize,Integer id,String skuName) {
        try {
            SkuQuery skuQuery = SkuQuery.builder().id(id).skuName(skuName).build();
            PageInfo<Sku> pageInfo = skuService.findByPage(page, pageSize,skuQuery);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }
}
